module top_module(
    input clk,
    input areset,    // Freshly brainwashed Lemmings walk left.
    input bump_left,
    input bump_right,
    input ground,
    input dig,
    output walk_left,
    output walk_right,
    output aaah,
    output digging ); 

    parameter L=0, R=1, FALL_L=2, FALL_R=3, DIG_L=4, DIG_R=5, SPLAT=6;
    reg [2:0] state, next_state;
    integer cnt = 0;
    
    always @(*) begin
        case(state)
            L: next_state = ground ? (dig ? DIG_L : (bump_left ? R : L)) : FALL_L;
            R: next_state = ground ? (dig ? DIG_R : (bump_right ? L : R)) : FALL_R;
            FALL_L: next_state = ground ? (cnt > 20 ? SPLAT : L) : FALL_L;
            FALL_R: next_state = ground ? (cnt > 20 ? SPLAT : R) : FALL_R;
            DIG_L: next_state = ground ? DIG_L : FALL_L;
            DIG_R: next_state = ground ? DIG_R : FALL_R;
            SPLAT: next_state = SPLAT;
            default: next_state = L;
        endcase
    end

    always @(posedge clk or posedge areset) begin
        if(areset) state = L;
        else state = next_state;
    end

    always @(posedge clk) begin
        case(next_state)
            FALL_L: cnt++;
            FALL_R: cnt++;
            default: cnt = 0;
        endcase
    end

    assign walk_left = (state == L);
    assign walk_right = (state == R);
    assign aaah = (state == FALL_L | state == FALL_R);
    assign digging = (state == DIG_L | state == DIG_R);

endmodule
